package com.ef.web.action;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;

import com.ef.constant.ConfirmMessage;
import com.ef.constant.StatusType;
import com.ef.domain.Department;
import com.ef.domain.School;
import com.ef.service.DepartmentService;
import com.ef.service.SchoolService;
import com.ef.util.CustomExample;

public class DepartmentAction extends BaseAction<Department> {

	private static final long serialVersionUID = 1L;
	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
	.getLog(DepartmentAction.class);
	
	private DepartmentService departmentService;
	
	private SchoolService schoolService;
	
	private Map<String,String> schoolList;
	
	@SuppressWarnings("serial")
	public String list(){
		this.listResult = departmentService.findByExample(
				new CustomExample<Department>(this.getModel()) {

					public void appendCondition(Criteria criteria) {
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}
	
	public String edit(){
		
		
		schoolList = getSchoolListData();
		return EDIT;
	}
	
	private Map<String,String> getSchoolListData(){
		
		Map<String,String> schoolMap = new LinkedHashMap<String,String>();
		
		List<School> schools = schoolService.findAll();
		
		for(School s:schools){
			if(s.getStatus()==StatusType.VALID)
				schoolMap.put(s.getId().toString(), s.getName());
		}
		
		return schoolMap;
	}
	
	public String save(){
		
		Department department  = this.getModel();
		departmentService.saveOrUpdate(department);
		this.setResult(ConfirmMessage.Department);
		schoolList = getSchoolListData();
		return EDIT;
	}
	public String delete(){
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			departmentService.delete(departmentService.findById(Long.valueOf(i)));
		}
		return DELETE;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public SchoolService getSchoolService() {
		return schoolService;
	}

	public void setSchoolService(SchoolService schoolService) {
		this.schoolService = schoolService;
	}

	public Map<String, String> getSchoolList() {
		return schoolList;
	}

	public void setSchoolList(Map<String, String> schoolList) {
		this.schoolList = schoolList;
	}
	
	

}
